<?php
class Ciuuser extends AppModel {
	var $name = 'Ciuuser';
	var $displayField = 'username';
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Ciuuserstate' => array(
			'className' => 'Ciuuserstate',
			'foreignKey' => 'ciuuserstate_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciuusertype' => array(
			'className' => 'Ciuusertype',
			'foreignKey' => 'ciuusertype_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciustudenttype' => array(
			'className' => 'Ciustudenttype',
			'foreignKey' => 'ciustudenttype_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciutimetable' => array(
			'className' => 'Ciutimetable',
			'foreignKey' => 'ciutimetable_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
		,
		'Ciuplan' => array(
			'className' => 'Ciuplan',
			'foreignKey' => 'ciuplan_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
		,
		'Ciuadmisionstate' => array(
			'className' => 'Ciuadmisionstate',
			'foreignKey' => 'ciuadmisionstate_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Ciucontract' => array(
			'className' => 'Ciucontract',
			'foreignKey' => 'ciuuser_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'CiucourseCiustudent' => array(
			'className' => 'CiucourseCiustudent',
			'foreignKey' => 'ciuuser_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Ciucourse' => array(
			'className' => 'Ciucourse',
			'foreignKey' => 'ciuuser_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'CiuexamCiuuser' => array(
			'className' => 'CiuexamCiuuser',
			'foreignKey' => 'ciuuser_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'CiuplanCiuuser' => array(
			'className' => 'CiuplanCiuuser',
			'foreignKey' => 'ciuuser_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
        
        
        /**
	 * Revisa que no exista otro curso del mismo lenguaje, nivel, horaario y periodo
	 * @param unknown_type $newRec
	 * @return boolean
	 */
	function isDuplicated( $newRec ){
		$result=true;
		$sql="select count(*) as qty from ciuusers where "
		." id <>". $newRec['id'] 
		." and document=". $newRec['document']
		;
	    $queryresult=$this->query($sql);
		$qty=$queryresult[0][0]['qty']; 	
		if($qty<1){
			$result=false;
		}
		return $result;		
	}
       function existeestudiante( $newRec ){
		$result=false;
		$sql="select count(*) as qty from ciuusers where "
                        ."ciuusertype_id = 1 "
                        ."and document = ".$newRec
                     ;
                
	        $queryresult=$this->query($sql);
		$qty=$queryresult[0][0]['qty'];

		if($qty>=1){
			$result=true;
		}
		return $result;		
	}
      function student_id( $newRec ){
		$qty=1;
		$sql="select id as qty from ciuusers where "
                        ."ciuusertype_id = 1 "
                        ."and document=".$newRec
                     ;
                
	        $queryresult=$this->query($sql);
		$qty=$queryresult[0][0]['qty'];
         
		return $qty;		
	}
}
